package view.bieudoindex;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import java.util.Map;

import javax.faces.event.ActionEvent;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.component.rich.RichPopup;
import oracle.adf.view.rich.component.rich.input.RichInputText;
import oracle.adf.view.rich.component.rich.output.RichOutputText;
import oracle.adf.view.rich.event.DialogEvent;
import oracle.adf.view.rich.event.PopupCanceledEvent;

import oracle.binding.BindingContainer;

import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewObject;

import view.common.Utils;

public class dmsanckBean {
    private RichPopup popupThemMoi;
    private RichPopup popupXem;
    private RichPopup popupValidate;
    private String Tensanck;
    private String Title;
    private RichOutputText inputtextOperator;
    private RichInputText txtTenSanCK = new RichInputText();

    public dmsanckBean() {
        super();
        if (Boolean.FALSE.equals(Utils.getElExpression("#{adfFacesContext.postback}"))) {
            DCIteratorBinding iterDmCK =
                Utils.getIteratorBinding("DmSanCkView1Iterator");
            iterDmCK.getViewObject().getViewCriteriaManager().clearViewCriterias();
            iterDmCK.executeQuery();

            DCIteratorBinding iterDmCkCheckExist =
                Utils.getIteratorBinding("DmSanCkCheckExistView1Iterator");
            iterDmCkCheckExist.getViewObject().getViewCriteriaManager().clearViewCriterias();
            iterDmCkCheckExist.executeQuery();
        }
    }

    public BindingContainer getBindings() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }

    public void ResetSearch() {
        try {
            DCIteratorBinding iterDmCK =
                Utils.getIteratorBinding("DmSanCkView1Iterator");
            iterDmCK.getViewObject().getViewCriteriaManager().clearViewCriterias();
            iterDmCK.executeQuery();

            DCIteratorBinding iterDmCkCheckExist =
                Utils.getIteratorBinding("DmSanCkCheckExistView1Iterator");
            iterDmCkCheckExist.getViewObject().getViewCriteriaManager().clearViewCriterias();
            iterDmCkCheckExist.executeQuery();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public void search() {
        try {
            DCIteratorBinding IteratorDmsan =
                Utils.getIteratorBinding("DmSanCkView1Iterator");
            ViewObject vo = IteratorDmsan.getViewObject();
            ViewCriteria vc =
                vo.getViewCriteriaManager().getViewCriteria("DmSanCkViewCriteria");
            vo.ensureVariableManager().setVariableValue("varTensan",
                                                        getTensanck());
            vo.getViewCriteriaManager().removeViewCriteria("DmSanCkViewCriteria");
            vo.getViewCriteriaManager().applyViewCriteria(vc);
            IteratorDmsan.executeQuery();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void create(ActionEvent actionEvent) {
        Utils.execute("CreateInsert");
        Utils.showPopup(popupThemMoi);
        inputtextOperator.setValue("0");
    }

    public void update(ActionEvent actionEvent) {
        if (Utils.getIteratorBinding("DmSanCkView1Iterator").getCurrentRow() ==
            null) {
            String meg = "Chưa có sàn chứng khoán nào.";
            Utils.showMessage(Utils.MSG_INFO, meg);
        } else {
            Utils.showPopup(popupThemMoi);
            inputtextOperator.setValue("1");
        }
    }

    public int checkExisted(String Tensan, String SanId) {
        DCIteratorBinding IteratorDmSanCk =
            Utils.getIteratorBinding("DmSanCkCheckExistView1Iterator");
        ViewObject vo = IteratorDmSanCk.getViewObject();
        ViewCriteria vc =
            vo.getViewCriteriaManager().getViewCriteria("DmSanCkCheckExistViewCriteria");
        vo.ensureVariableManager().setVariableValue("varTensan", Tensan);
        vo.ensureVariableManager().setVariableValue("varCheckId", SanId);
        vo.getViewCriteriaManager().removeViewCriteria("DmSanCkCheckExistViewCriteria");
        vo.getViewCriteriaManager().applyViewCriteria(vc);
        vo.setRangeSize(1);
        IteratorDmSanCk.executeQuery();
        if (vo.getAllRowsInRange().length > 0) {
            return 1;
        }
        return 0;
    }

    public void commitDialog(DialogEvent dialogEvent) {
        try {
            int operator = inputtextOperator.getValue().equals("1") ? 1 : 0;
            DCIteratorBinding iterDmsanCk =
                Utils.getIteratorBinding("DmSanCkView1Iterator");
            String SanId = "";
            String TenSanMoi = "";
            TenSanMoi = txtTenSanCK.getValue().toString();
            if (operator == 0) {
                SanId = "";
            } else {
                SanId =
                        iterDmsanCk.getCurrentRow().getAttribute("Id").toString();
            }
            if (checkExisted(TenSanMoi, SanId) > 0) {
                String meg =
                    "Sàn chứng khoán đã tồn tại. Bạn phải nhập tên sàn chứng khoán khác";
                Utils.showMessage(Utils.MSG_ERROR, meg);
            } else {
                String rowKey =
                    iterDmsanCk.getCurrentRow().getKey().toStringFormat(false);
                Utils.execute("Commit");
                Utils.iteratorRefresh("DmSanCkView1Iterator");
                Utils.iteratorRefresh("DmSanCkCheckExistView1Iterator");
                iterDmsanCk.setCurrentRowWithKey(rowKey);
                if (operator == 0) {
                    ViewObject vo = iterDmsanCk.getViewObject();
                    ViewCriteria vc =
                        vo.getViewCriteriaManager().getViewCriteria("DmSanCkViewCriteria");
                    vo.ensureVariableManager().setVariableValue("varTensan",
                                                                "%");
                    vo.getViewCriteriaManager().removeViewCriteria("DmSanCkViewCriteria");
                    vo.getViewCriteriaManager().applyViewCriteria(vc);
                    iterDmsanCk.executeQuery();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void cancelDiaglog(PopupCanceledEvent popupCanceledEvent) {
        DCIteratorBinding iterDmsanCk =
            Utils.getIteratorBinding("DmSanCkView1Iterator");
        String rowKey =
            iterDmsanCk.getCurrentRow().getKey().toStringFormat(false);
        Utils.execute("Rollback");
        iterDmsanCk.setCurrentRowWithKey(rowKey);
    }

    public void deleteDiaglog(DialogEvent dialogEvent) {
        try {
            if (Utils.getIteratorBinding("DmSanCkView1Iterator").getCurrentRow() ==
                null) {
                String meg = "Chưa có sàn chứng khoán nào.";
                Utils.showMessage(Utils.MSG_INFO, meg);
            } else {
                DCIteratorBinding iterDmsanCk =
                    Utils.getIteratorBinding("DmSanCkView1Iterator");
                Object SanID = iterDmsanCk.getCurrentRow().getAttribute("Id");
                if (SanID.equals(1) || SanID.equals(2)) {
                    String messg = "Không được phép xóa sàn chứng khoán này.";
                    Utils.showMessage(Utils.MSG_ERROR, messg);
                } else {
                    Utils.execute("Delete");
                    List constrainExist = Utils.execute("Commit");
                    if (constrainExist != null &&
                        constrainExist.toString().contains("VN_HASTC_INDEX_SAN_FK")) {
                        Utils.showPopup(popupValidate);
                        Utils.iteratorRefresh("DmSanCkView1Iterator");
                        Utils.iteratorRefresh("DmSanCkCheckExistView1Iterator");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setPopupThemMoi(RichPopup popupThemMoi) {
        this.popupThemMoi = popupThemMoi;
    }

    public RichPopup getPopupThemMoi() {
        return popupThemMoi;
    }

    public void setTensanck(String Tensanck) {
        this.Tensanck = Tensanck;
    }

    public String getTensanck() {
        return Tensanck;
    }

    public void setInputtextOperator(RichOutputText inputtextOperator) {
        this.inputtextOperator = inputtextOperator;
    }

    public RichOutputText getInputtextOperator() {
        return inputtextOperator;
    }

    public void setPopupXem(RichPopup popupXem) {
        this.popupXem = popupXem;
    }

    public RichPopup getPopupXem() {
        return popupXem;
    }

    public void setTitle(String Title) {
        this.Title = Title;
    }

    public String getTitle() {
        Title = "Thêm mới sàn chứng khoán";
        int operator = inputtextOperator.getValue().equals("1") ? 1 : 0;
        if (operator == 0) {
            Title = "Thêm mới sàn chứng khoán";
        } else {
            Title = "Sửa sàn chứng khoán";
        }
        return Title;
    }

    public void setTxtTenSanCK(RichInputText txtTenSanCK) {
        this.txtTenSanCK = txtTenSanCK;
    }

    public RichInputText getTxtTenSanCK() {
        return txtTenSanCK;
    }

    public void setPopupValidate(RichPopup popupValidate) {
        this.popupValidate = popupValidate;
    }

    public RichPopup getPopupValidate() {
        return popupValidate;
    }
}
